#[allow(unused)]
pub fn solute() {
    let contents = std::fs::read_to_string("./day03/input.txt").unwrap();
    let lines: Vec<&str> = contents.lines().collect();

    let check_isolate = |row: usize, begin: usize, end: usize| -> bool {
        println!("{}-{}-{}", row, begin, end);
        // lines[row][begin..end] is digit str
        // Check lines[row-1][begin-1..end+1]
        if row > 0 {
            let line = lines[row - 1].as_bytes();
            // Check lines[row - 1][begin - 1]
            if begin > 0 {
                if line[begin - 1] != b'.' {
                    return false;
                }
            }
            // Check lines[row - 1][end]
            if end < line.len() {
                if line[end] != b'.' {
                    return false;
                }
            }
            // Check lines[row - 1][begin..end]
            for &byte in (&line[begin..end]).iter() {
                if byte != b'.' {
                    return false;
                }
            }
        }

        // Check lines[row][begin-1] & lines[row][end+1]
        let line = lines[row].as_bytes();
        if begin > 0 {
            if line[begin-1] != b'.' {
                return false;
            }
        }
        if end < line.len() {
            if line[end] != b'.' {
                return false;
            }
        }

        // Check lines[row+1][begin-1..end+1]
        if row < lines.len() - 1 {
            let line = lines[row + 1].as_bytes();
            // Check lines[row + 1][begin - 1]
            if begin > 0 {
                if line[begin - 1] != b'.' {
                    return false;
                }
            }
            // Check lines[row + 1][end]
            if end < line.len() {
                if line[end] != b'.' {
                    return false;
                }
            }
            // Check lines[row + 1][begin..end]
            for &byte in (&line[begin..end]).iter() {
                if byte != b'.' {
                    return false;
                }
            }
        }

        true
    };

    let mut sum: u32 = 0;
    for (row, &line) in lines.iter().enumerate() {
        let line = line.as_bytes();
        let mut begin: usize = 0;

        while begin < line.len() {
            let ch: char = line[begin] as char;
            if !ch.is_digit(10) {
                begin += 1;
                continue;
            }

            // line[begin] is beginng of a digit str, find next not digit.
            let mut end: usize = begin + 1;
            loop {
                if end >= line.len() || !(line[end] as char).is_digit(10) {
                    break;
                }
                end += 1;
            }

            // line[begin, end) is digit str
            // Check character around line[begin, end)
            if !check_isolate(row, begin, end) {
                let value: u32 = (&lines[row][begin..end]).parse().unwrap();
                sum += value;
            } else {
                let value: u32 = (&lines[row][begin..end]).parse().unwrap();
                println!("{value}");
            }

            begin = end + 1;
        }
    }

    println!("{sum}");
}


